package mymqsender.web.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import mymqsender.services.MqSender;
import mymqsender.web.resps.CommonResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * create by caoqg on 2021/7/27
 * 功能说明:
 */
@RestController
@RequestMapping("/api")
@Slf4j
public class MyTest {

    @Autowired
    private MqSender mqSender;

    @RequestMapping("/test")
    public CommonResp<Object> testOne(){
        return CommonResp.errorMsg("当前接口未实现");
    }

    @RequestMapping("/send")
    public CommonResp<String> testSend(@RequestParam("uid") String uid,@RequestParam("msg") String msg){
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode obj = mapper.createObjectNode();
        obj.put("uid", uid);
        obj.put("msg", msg);
        String ts = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        obj.put("ts", ts);
        String msgBody = obj.toString();
        log.info("startSend,msg:{}", msgBody);
        mqSender.sendMsg(msgBody);
        log.info("endSend,msg:{}", msgBody);
        CommonResp<String> result = CommonResp.succ(msgBody);
        return result;
    }
}
